﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using memo;
using memoVM;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for SubTopicSelectorWindow.xaml
    /// </summary>
    public partial class SubTopicSelectorWindow : Window
    {
        private string ImagePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "/Images/";

        private int NumberOfSubTopics; //topic darabszámának a megadása
        private const int NumberOfColumns = 4; // oszlopok száma
        private int NumberOfRows; // sorok száma

        private StackPanel[] DynamicStackPanel; //Egy subtopichoz tartozó controlokat tartalmazó panel
        private Label[] DynamicLabel; //subtopic neve
        private Image[] DynamicImage; //subtopichoz tartozó ikon

        private Grid[] DynamicProgressBarPanel; //Progressbarok és a hozzájuk tartozó feliratok tartalmazó panel
        private ProgressBar[] DynamicProgressBar; //subtopicok tudási szintjét jelző progressbar
        private TextBlock[] DynamicProgressBarText; //progressbarhoz tartozó felirat megjelenítése szolgáló textblock

        private ViewModel _ViewModel;

        public SubTopicSelectorWindow()
        {
            InitializeComponent();

            _ViewModel = ViewModel.Instance;
            NumberOfSubTopics = _ViewModel.GetNumberOfSubtopics();

            DynamicStackPanel = new StackPanel[NumberOfSubTopics];
            DynamicLabel = new Label[NumberOfSubTopics];
            DynamicImage = new Image[NumberOfSubTopics];

            DynamicProgressBarPanel = new Grid[NumberOfSubTopics];
            DynamicProgressBar = new ProgressBar[NumberOfSubTopics];
            DynamicProgressBarText = new TextBlock[NumberOfSubTopics];

            Init();
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            TopicSelectorWindow Window = new TopicSelectorWindow();
            Window.Show();
            this.Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            SubTopicEditorWindow Window = new SubTopicEditorWindow(true);
            Window.ShowDialog();
            SubTopicSelectorWindow window = new SubTopicSelectorWindow();
            window.Show();
            Close();
        }

        private void SubtopicRight_Click(object sender, MouseButtonEventArgs e)
        {
            //konverzió hogy elérjük a tag propertyt amiben a topic sorszámát tároljuk 
            StackPanel obj = sender as StackPanel;
            _ViewModel.SelectedSubtopic = int.Parse(obj.Tag.ToString());

            ContextMenu cm = this.FindResource("cmButton") as ContextMenu;
            cm.PlacementTarget = sender as StackPanel;
            cm.IsOpen = true;
        }

        private void SubtopicLeft_Click(object sender, MouseButtonEventArgs e)
        {
            //konverzió hogy elérjük a tag propertyt amiben a subtopic sorszámát tároljuk 
            StackPanel obj = sender as StackPanel;
            _ViewModel.SelectedSubtopic = int.Parse(obj.Tag.ToString());
            ContentWindow Window = new ContentWindow();
            Window.Show();
            this.Close();
        }

        private void SubtopicEdit_Click(object Sender, RoutedEventArgs e)
        {
            SubTopicEditorWindow Window = new SubTopicEditorWindow(false);
            Window.ShowDialog();
            SubTopicSelectorWindow window = new SubTopicSelectorWindow();
            window.Show();
            Close();
        }

        private void SubtopicDelete_Click(object Sender, RoutedEventArgs e)
        {
            _ViewModel.DeleteSelectedSubtopic();
            SubTopicSelectorWindow window = new SubTopicSelectorWindow();
            window.Show();
            Close();
        }

        public void Init()
        {
            int progressBarHeight = 25;
            int labelHeight = 40;
            int labelFontSize = 20;
            SubTopic mySubtopic;

            //a szükséges sorok számának kiszámítása
            NumberOfRows = (int)Math.Ceiling(((double)NumberOfSubTopics / NumberOfColumns)); // ¯\(ツ)/¯

            RowDefinition[] rowDef = new RowDefinition[NumberOfRows];

            //a szükséges gird sorok létrehozása
            for (int i = 4; i < NumberOfRows; i++)
            {
                rowDef[i] = new RowDefinition();
                rowDef[i].Height = new GridLength(1, GridUnitType.Star);
                SubTopicGrid.RowDefinitions.Add(rowDef[i]);
            }

            //topikokhoz tartozó controlok létrehozása
            for (int i = 0; i < NumberOfSubTopics; i++)
            {
                mySubtopic = _ViewModel.GetSubtopicAtIndex(i);
                BrushConverter bc = new BrushConverter();

                //SubTopichoz tartozó controlokat tartalmazó panel beállítása
                DynamicStackPanel[i] = new StackPanel();
                DynamicStackPanel[i].Background = (Brush)bc.ConvertFromString("#ACD661"); 
                //DynamicStackPanel[i].Background = Brushes.LightGray; 
                DynamicStackPanel[i].Tag = i; // topic sorszámának letárolása a hozzá tartozó subtopic betöltéséhez
                DynamicStackPanel[i].Margin = new Thickness(8); // Subtopic panelek közötti távolság
                DynamicStackPanel[i].Orientation = Orientation.Vertical;
                DynamicStackPanel[i].MouseLeftButtonDown += SubtopicLeft_Click;
                DynamicStackPanel[i].MouseRightButtonDown += SubtopicRight_Click;
                Grid.SetRow(DynamicStackPanel[i], i / (SubTopicGrid.ColumnDefinitions.Count));
                Grid.SetColumn(DynamicStackPanel[i], i % (SubTopicGrid.ColumnDefinitions.Count));

                //SubTopicnak a nevéhez tartozó label beállítása
                DynamicLabel[i] = new Label();
                DynamicLabel[i].Content = mySubtopic.Title; //Itt kell megadni a subtopic nevét //string
                DynamicLabel[i].Height = labelHeight;
                DynamicLabel[i].FontSize = labelFontSize;
                DynamicLabel[i].FontWeight = FontWeights.Bold;
                DynamicLabel[i].Foreground = Brushes.White;
                DynamicLabel[i].VerticalAlignment = VerticalAlignment.Top;
                DynamicLabel[i].HorizontalAlignment = HorizontalAlignment.Center;
                DynamicStackPanel[i].Children.Add(DynamicLabel[i]);

                //SubTopicnak az ikonjának beállítása
                DynamicImage[i] = new Image();
                //string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "/Images/ASD" + rnd.Next(0, 2) + ".jpg"; //(╯°□°）╯︵ ┻━┻  //ez csak próba, ↓ ezt használd 
                string path = ImagePath + mySubtopic.Icon;  //kép betöltése a subtopichoz // EZT HASZNÁLD!!!  //stringben a képhez vezető path  // ┬─┬﻿ ノ(゜-゜ノ)
                DynamicImage[i].Source = (ImageSource)ImageLoader(path);
                DynamicImage[i].Margin = new Thickness(5, 0, 5, 5);
                DynamicImage[i].Stretch = Stretch.Fill;
                DynamicImage[i].VerticalAlignment = VerticalAlignment.Center;
                DynamicImage[i].HorizontalAlignment = HorizontalAlignment.Center;
                DynamicImage[i].ToolTip = mySubtopic.ShortDescription;
                //DynamicImage[i].Margin = new Thickness(0, labelHeight, 0, progressBarHeight);
                DynamicStackPanel[i].Children.Add(DynamicImage[i]);

                //Progressbart tartalmazó panel létrehozása
                DynamicProgressBarPanel[i] = new Grid();
                DynamicProgressBarPanel[i].Margin = new Thickness(5, 0, 5, 5);
                DynamicProgressBarPanel[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicProgressBarPanel[i].VerticalAlignment = VerticalAlignment.Bottom;

                //Progressbar létrehozása
                DynamicProgressBar[i] = new ProgressBar();
                DynamicProgressBar[i].Minimum = 0;
                DynamicProgressBar[i].Maximum = 100;
                DynamicProgressBar[i].Value = mySubtopic.GetKnowledgePercent(); //itt kell értéket adni a progressbarnak // int 0-100 intervallumon
                DynamicProgressBar[i].Height = progressBarHeight;
                DynamicProgressBar[i].VerticalAlignment = VerticalAlignment.Bottom;
                DynamicProgressBar[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicProgressBarPanel[i].Children.Add(DynamicProgressBar[i]);

                //Progressbar feliratának beállítása //itt nem kell állítani semmit sem
                DynamicProgressBarText[i] = new TextBlock();
                DynamicProgressBarText[i].Text = DynamicProgressBar[i].Value.ToString() + "%";
                DynamicProgressBarText[i].TextAlignment = TextAlignment.Center;
                DynamicProgressBarText[i].Height = progressBarHeight;
                DynamicProgressBarText[i].VerticalAlignment = VerticalAlignment.Stretch;
                DynamicProgressBarText[i].HorizontalAlignment = HorizontalAlignment.Stretch;
                DynamicProgressBarPanel[i].Children.Add(DynamicProgressBarText[i]);

                DynamicStackPanel[i].Children.Add(DynamicProgressBarPanel[i]);
                DynamicStackPanel[i].Cursor = Cursors.Hand;
                SubTopicGrid.Children.Add(DynamicStackPanel[i]);
            }
        }

        public object ImageLoader(string path)
        {
            var bitmap = new BitmapImage();
            var stream = File.OpenRead(path);
            bitmap.BeginInit();
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            stream.Close();
            stream.Dispose();
            return bitmap;
        }
    }
}
